package com.ocean.admin.controller;

import com.ocean.core.model.vo.FileUploadVo;
import com.ocean.core.model.vo.VideoUploadVo;
import com.ocean.core.util.response.ResultSet;
import com.ocean.core.util.upload.FileUtil;
import com.ocean.core.util.upload.VideoUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 公共接口
 *
 * @author zhang
 * @since 2022/8/31 21:12
 **/
@Tag(name = "公共接口")
@RestController
@RequestMapping("common")
public class CommonController {

    /**
     * 文件上传
     *
     * @param file file
     * @return 结果
     */
    @Operation(summary = "文件上传")
    @PostMapping(value = "upload")
    public ResultSet<FileUploadVo> upload(@RequestPart MultipartFile file) {
        return ResultSet.ok(FileUtil.upload(file));
    }

    /**
     * 视频上传
     *
     * @param file file
     * @return 结果
     */
    @Operation(summary = "视频上传")
    @PostMapping(value = "uploadVideo")
    public ResultSet<VideoUploadVo> uploadVideo(@RequestPart MultipartFile file) throws Exception {
        return ResultSet.ok(VideoUtil.uploadVideo(file));
    }

    /**
     * 图片预览
     *
     * @param image    图片地址
     * @param response response
     */
    @Operation(summary = "图片预览")
    @GetMapping("/static/{date}/{image}")
    public void view(@PathVariable String date, @PathVariable String image, HttpServletResponse response) {
        FileUtil.view(date, image, response);
    }
}
